<template>
	<view>
		<!-- 搜素框 -->
		<view class="search-box">
			<!-- 使用uni-ui提供的搜索组件 -->
			<uni-search-bar @input="input" placeholder="请输入搜索内容" :radius="100" cancelButton="none"
				class="uni-search-bar">
			</uni-search-bar>
		</view>
		<!-- 搜素列表 -->
		<view class="sugg-list" v-if="searchResult.length !== 0">
			<view class="sugg-item" v-for="(item,index) in searchResult" :key="index"
				@click="gotoDetail(item.goods_id)">
				<view class="goods_name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>

		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHistoryList"></uni-icons>
			</view>
			<!-- 历史列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item ,index) in historys" :key="index" @click="gotoGoodsList(item)">
				</uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 防抖延时器
				timer: null,
				// 搜素关键词
				kw: '',
				// 搜索结果列表
				searchResult: [],
				// 搜索关键词历史记录
				historyList: []
			};
		},
		computed: {
			historys() {
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			// 发起搜索请求
			async getSearchList() {
				if (this.kw === '') {
					this.searchResult = [];
					return
				}
				// 发起请求,获取搜索列表
				const {
					data: res
				} = await uni.$http.get('/goods/qSearch', {
					query: this.kw
				});
				if (res.meta.status !== 200) return uni.$showMsg();
				this.searchResult = res.message;
				this.saveSearchHistory();
			},
			// 搜索框输入input事件
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e;
					this.getSearchList()
				}, 500)
			},
			// 跳转商品详情页
			gotoDetail(goods_id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			// 保存搜素关键词
			saveSearchHistory() {
				const set = new Set(this.historyList);
				set.delete(this.kw);
				set.add(this.kw);
				this.historyList = Array.from(set);
				// 将历史搜素记录持久存到本地
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			// 清空历史搜索
			cleanHistoryList() {
				this.historyList = [];
				uni.removeStorageSync('kw')
			},
			gotoGoodsList(kw) {

				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw

				})
			}
		},
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;

		.uni-search-bar {
			background-color: #c00000;
		}
	}

	.sugg-list {
		padding: 0 20rpx;

		.sugg-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 24rpx;
			padding: 26rpx 0;
			border-bottom: 2rpx solid #e3e3e3;

			.goods_name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 6rpx;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
			height: 40px;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				background-color: #efefef;
				color: #000;
				margin: 5px 5px 0 0;
			}
		}
	}
</style>
